package com.jy.adscm.system.controller;

import com.jy.adscm.bean.system.JyMenu;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.support.SimpleResponse;
import com.jy.adscm.system.service.JyMenuService;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author yyy
 * @ClassName AdscmBaseController
 * @Description 主要用于访问用户的URL，其他资源路径有权限限制，所以这个单独领出来...
 * @date 2018/8/2 17:40
 */
@RestController
@CrossOrigin
public class AdscmBaseController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    JyMenuService jyMenuService;

    @Value("${spring.http.multipart.location}")
    String location;

    /**
     * 获取指定用户的能访问的所有URL
     * @param request
     * @return
     */
//    @RequestMapping("/users/urls")
//    public SimpleResponse getUsersUrls(HttpServletRequest request) throws AdscmSystemException {
//        String tokenId = request.getParameter("tokenId");
//
//        List<JyMenu> menus = jyMenuService.getUsersUrls(tokenId);
//
//        return new SimpleResponse("200","访问成功",menus);
//    }


    /*
     * 采用file.Transto 来保存上传的文件
     */
    @RequestMapping("/fileUpload")
    public SimpleResponse  fileUpload(HttpServletRequest request) throws AdscmSystemException {


        List<String> paths = null;

        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());

        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

            paths = AdscmSystemUtils.getInstance().uploadFile(multiRequest,location);
        }



        return new SimpleResponse("200","访问成功",paths);
    }
}
